package com.nginx.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nginx.web.domain.ConfFileQo;
import com.nginx.web.entity.ConfFile;
import com.nginx.web.entity.Config;
import com.nginx.web.mapper.ConfFileMapper;
import com.nginx.web.service.ConfFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nginx.web.util.CmdUtil;
import com.nginx.web.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyc
 * @since 2025-01-13
 */
@Service
public class ConfFileServiceImpl extends ServiceImpl<ConfFileMapper, ConfFile> implements ConfFileService {

    @Autowired
    private ConfigServiceImpl configService;


    public Page<ConfFile> listConfFile(ConfFileQo confFileQo) {
        LambdaQueryWrapper<ConfFile> wrapper = Wrappers.lambdaQuery(ConfFile.class)
                .orderByAsc(ConfFile::getSort);
        Page<ConfFile> page = new Page<>(confFileQo.getPage() , confFileQo.getLimit());
        this.page(page,wrapper);
        return page;
    }

    public void updateConfFile(ConfFile confFile) {
        updateById(confFile);
    }

    public void readeConfFile() {
        Config config = configService.getOne(Wrappers.lambdaQuery(Config.class)
                .eq(Config::getKey, Constants.nginx_path));
        String path = config.getValue();
        File dir = new File(path);
        List<ConfFile> list = new ArrayList<>();
        checkFile(dir , dir , list);
        for (ConfFile confFile : list) {
            ConfFile one = getOne(Wrappers.lambdaQuery(ConfFile.class)
                    .eq(ConfFile::getPath, confFile.getPath()));
            if(one == null){
                this.save(confFile);
            }else{
                one.setContent(confFile.getContent());
                this.updateById(confFile);
            }
        }
    }

    public void checkFile(File topDir,File dir , List<ConfFile> list){
        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            for (File file : files) {
                checkFile(topDir , file , list);
            }
        }
        if(dir.isFile() && StrUtil.equals(FileUtil.getSuffix(dir) , "conf")){
            String absolutePath = dir.getAbsolutePath().replace( topDir.getAbsolutePath() , "");
            ConfFile confFile = new ConfFile();
            confFile.setName(dir.getName());
            confFile.setPath(absolutePath);
            confFile.setContent(FileUtil.readUtf8String(dir));
            list.add(confFile);
        }
    }

    public String startNginx() {
        Config config = configService.getOne(Wrappers.lambdaQuery(Config.class)
                .eq(Config::getKey, Constants.nginx_path));
        return CmdUtil.startNginx(config.getValue());
    }

    public String reloadNginx() {
        Config config = configService.getOne(Wrappers.lambdaQuery(Config.class)
                .eq(Config::getKey, Constants.nginx_path));
        return CmdUtil.reloadNginx(config.getValue());
    }

    public String checkNginx() {
        Config config = configService.getOne(Wrappers.lambdaQuery(Config.class)
                .eq(Config::getKey, Constants.nginx_path));
        return CmdUtil.checkNginx(config.getValue());
    }

    public void writeConfFile() {
        Config config = configService.getOne(Wrappers.lambdaQuery(Config.class)
                .eq(Config::getKey, Constants.nginx_path));
        String value = config.getValue();
        List<ConfFile> list = this.list();
        if(CollectionUtil.isEmpty(list)){
            return ;
        }
        for (ConfFile confFile : list) {
            String path = value + confFile.getPath();
            String content = confFile.getContent();
            FileUtil.writeString(content , path , "UTF-8");
        }
    }
}
